/**
 * 百人场牛牛新版
 * @author Troj.an
 *
 */
class P100GameSceneViewNew extends V.GameView
{
    public playerView0: P100NNPlayerView;

    public bitlabTotal0: eui.BitmapLabel;
    public groupSelfBetVal0: eui.Group;
    public labSelfBetVal0: eui.Label;

    public bitlabTotal1: eui.BitmapLabel;
    public groupSelfBetVal1: eui.Group;
    public labSelfBetVal1: eui.Label;

    public bitlabTotal2: eui.BitmapLabel;
    public groupSelfBetVal2: eui.Group;
    public labSelfBetVal2: eui.Label;

    public bitlabTotal3: eui.BitmapLabel;
    public groupSelfBetVal3: eui.Group;
    public labSelfBetVal3: eui.Label;;

    public groupResult: eui.Group;
    public labSelfRes: eui.Label;
    public labBankerRes: eui.Label;

    public groupBG: eui.Group;
    public groupLeftBtns: eui.Group;
    public groupCenter: eui.Group;

    public sendCardPoint: eui.Image;

    public groupColor0: eui.Group;
    public groupColor1: eui.Group;
    public groupColor2: eui.Group;
    public groupColor3: eui.Group;

    public groupBetArea0: eui.Group;
    public groupBetArea1: eui.Group;
    public groupBetArea2: eui.Group;
    public groupBetArea3: eui.Group;

    public showCard0: eui.Component;
    public showCard1: eui.Component;
    public showCard2: eui.Component;
    public showCard3: eui.Component;

    public imgCardType0: eui.Image;
    public imgCardType1: eui.Image;
    public imgCardType2: eui.Image;
    public imgCardType3: eui.Image;
    public imgCardType4: eui.Image;

    public groupBanker: eui.Group;
    public showCardBanker: eui.Component;
    public groupSelfInfo: eui.Group;
    public trendButton: how.Button;
    public backButton: how.Button;
    public tabBarOperate: eui.TabBar;

    public groupGameStatus: eui.Group; // 游戏状态组
    public labStatus: eui.Label;

    public labNickName: eui.Label;
    public labUserGold: eui.Label;
    public labUserScore: eui.Label;

    public groupHead: eui.Group;
    public headBorder: eui.Image;
    public headLoader: eui.Image;
    public labBankerName: eui.Label;
    public labBankerScore: eui.Label;
    public btnReqBanker: how.Button;

    // 下注区域结算信息
    public groupAreaRes0: eui.Group;
    public groupAreaRes1: eui.Group;
    public groupAreaRes2: eui.Group;
    public groupAreaRes3: eui.Group;

    public labAreaMulti0: eui.Label;
    public labAreaMulti1: eui.Label;
    public labAreaMulti2: eui.Label;
    public labAreaMulti3: eui.Label;

    public labAreaNoBet0: eui.Label;
    public labAreaNoBet1: eui.Label;
    public labAreaNoBet2: eui.Label;
    public labAreaNoBet3: eui.Label;

    public labAreaScore0: eui.Label;
    public labAreaScore1: eui.Label;
    public labAreaScore2: eui.Label;
    public labAreaScore3: eui.Label;

    // 用户座位控件
    public userButton0: how.Button;
    public userButton1: how.Button;
    public userButton2: how.Button;
    public userButton3: how.Button;
    public userButton4: how.Button;
    public userButton5: how.Button;
    public userButtonOther: how.Button;
    public btnCoinPoint: how.Button;
    // 下注提示
    public groupJettonTips: eui.Group;
    public labJettonTips: eui.Label;

    // 数据
    private intervalSign: number; // 一个egret.setIntervalSign的返回的标记
    private countDownTime: number; // 当前倒计时剩余时间
    private currentStatusTips: string; // 当前状态的提示文字
    private CARD_NUMS: number = 5;                              // 每组牌的张数
    private tabIndexValue: number[] = [];
    private showCardArray: eui.Component[] = [];                // 存放手牌展示组件的数组
    private showCardTypeArray: eui.Image[] = [];            // 存放手牌牌型展示组件的数组
    private groupBetAreaArray: eui.Group[] = [];                // 下注区域背景图
    private groupColorAreaArray: eui.Group[] = [];               // 区域组下注用
    private betCoinAreaArray: eui.Image[][] = [[], [], [], []]; // 管理下注显示的金币
    private totalBetBitLabArray: eui.BitmapLabel[] = [];        // 每个下注区显示当前下注总值控件
    private selfBetLabelArray: eui.Label[] = [];                // 每个下注区域显示自己的当前下注控件
    private timeoutSignArray: Array<any> = null;                    // 超时标记缓存，用于Destroy时，集中销毁超时，确保所有超时都被销毁
    // 用于发牌动画展示
    private currentCardIdx: number = 0;         // 当前发到哪一张牌
    private currentCardGroupIdx: number = 0;    // 当前发到哪一组牌
    private sendCardInterval: number = 0;       // 发牌间隔标志
    // 用于发完牌开牌动作
    private currentColorGroupIdx: number = 0;         // 当前展示到哪一组牌
    private showCardInterval: number = 0;       // 展示牌间隔标志
    // 用户座位数组
    private userSeatArray: how.Button[] = [];
    // 记录下注区域被哪些用户下过注
    private userBetAreaRecArray: number[][] = [[], [], [], []];
    // 游戏特效面板
    private gameEffectPanel: PublicGameEffectPanel;

    public constructor() 
    {
        super();
        this.MAX_PLAYER = 2;
        this.skinName = "P100GameSceneSkin";
        this.Res = DataConfig.ResConfig[DataConfig.KIND_ID_NN100];
    }

    protected childrenCreated(): void
    {        
        this.initUI();
        how.SoundManager.playMusic("nn_bg_mp3");
        this.initPlayer();
        this.timeoutSignArray = new Array();
        this.tabIndexValue = [100, 1000, 10000, 100000, 500000];
        this.showCardArray = [this.showCardBanker, this.showCard0, this.showCard1, this.showCard2, this.showCard3];
        this.showCardTypeArray = [this.imgCardType4, this.imgCardType0, this.imgCardType1, this.imgCardType2, this.imgCardType3];
        this.groupBetAreaArray = [this.groupBetArea0, this.groupBetArea1, this.groupBetArea2, this.groupBetArea3];
        this.groupColorAreaArray = [this.groupColor0, this.groupColor1, this.groupColor2, this.groupColor3];

        for (var i: number = 0; i < this.groupColorAreaArray.length; ++i)
        {
            var group: eui.Group = this.groupColorAreaArray[i];
            group.addEventListener(egret.TouchEvent.TOUCH_TAP, this.betAreaClick, this);
        }
        this.totalBetBitLabArray = [this.bitlabTotal0, this.bitlabTotal1, this.bitlabTotal2, this.bitlabTotal3];
        this.selfBetLabelArray = [this.labSelfBetVal0, this.labSelfBetVal1, this.labSelfBetVal2, this.labSelfBetVal3];

        this.trendButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTrendButtonBtnClick, this);
        this.btnReqBanker.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onReqBankerClick, this);
        this.backButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBackButton, this);
        this.userButtonOther.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onUserBtnClick, this);
        this.userSeatArray = [this.userButton0, this.userButton1, this.userButton2, this.userButton3, this.userButton4, this.userButton5, this.userButtonOther, this.btnCoinPoint];

        // 显示自己头像
        (<P100NNPlayerView>this.playerList[0]).showP100NNUser();
        // 设置庄家默认为系统
        this.setBankerDataAndShow(65535);
        // 设置状态显示
        this.setGameStateTips(13, LanguageConfig.p100TakeRest);
    }

     /** 收到网络消息 */
    public onNetData(s:number, d:any):void
    {
        switch(s)
        {
            case 99: // 游戏空闲
            {
                this.timeoutSignArray["gameFree"] = egret.setTimeout(this.gameFree, this, 1000);

                break;
            }
            case 100: // 游戏开始
            {
                P100GameSceneData.getInstance().setJettonLimit(d);
                this.updateJettonPanel(P100GameSceneData.getInstance(), true);

                break;
            }
            case 109: // 开始下注
            {
                // 设置状态显示
                this.setGameStateTips(d.cbTimeLeave, LanguageConfig.gameTips_beginBetOn);

                break;
            }
            case 101: // 用户下注
            {
                var nUserIdx: number = 0;
                P100GameSceneData.getInstance().setJettonInfo(d);
                if (d.wChairID == D.App.userData.chairID)
                {
                    this.updateJettonPanel(P100GameSceneData.getInstance(), false);
                    nUserIdx = 7;
                }
                else
                {
                    nUserIdx = 6;
                }
                this.playerPlaceJetton(d, nUserIdx);
                this.upJettonInfo(d, P100GameSceneData.getInstance());

                break;
            }
            case 107: // 下注失败
            {
                this.updateJettonPanel(P100GameSceneData.getInstance(), false);
                break;
            }
            case 102: // 游戏结束
            {
                this.hideJettonPanel();
                P100GameSceneData.getInstance().setGameEndRes(d);                       // 设置游戏结束信息
                this.showOpenCard();
                break;
            }
        }
    }

    public showPlayer(c:number):void{}

    // 清理界面
    public initUI(): void
    {
        //this.hideJettonPanel();

        // 隐藏牌型显示
        for (var i: number = 0; i < this.showCardTypeArray.length; ++i)
        {
            this.showCardTypeArray[i].source = "";
            this.showCardTypeArray[i].visible = false;
        }
        // 隐藏牌 牌数据
        for (var i: number = 0; i < this.showCardArray.length; ++i)
        {
            var componentCard: eui.Component = this.showCardArray[i];
            for (var j: number = 0; j < this.CARD_NUMS; ++j)
            {
                var card: eui.Image = componentCard["card" + j.toString()];
                card.source = "card_0_png";
                card.visible = false;
            }
        }
        
        // 初始化下注的金币
        for (var i: number = 0; i < this.betCoinAreaArray.length; ++i)
        {
            var imgBetCoinArray: eui.Image[] = this.betCoinAreaArray[i];
            for (var j: number = 0; j < imgBetCoinArray.length; ++j)
            {
                var imgBetCoin: eui.Image = imgBetCoinArray[j];
                this.groupCenter.removeChild(imgBetCoin);
            }
            this.betCoinAreaArray[i].length = 0;
        }
        // 初始化全部下注统计显示的值
        for (var i: number = 0; i < this.totalBetBitLabArray.length; ++i)
        {
            var curBitLab: eui.BitmapLabel = this.totalBetBitLabArray[i];
            curBitLab.text = "";
            curBitLab.visible = false;
        }
        // 初始化自己下注统计显示的值
        for (var i: number = 0; i < this.selfBetLabelArray.length; ++i)
        {
            var selfLabel: eui.Label = this.selfBetLabelArray[i];
            selfLabel.parent.visible = false;
            selfLabel.text = "";
        }
        // 初始化区域计算信息
        for (var i: number = 0; i < this.groupColorAreaArray.length; ++i)
        {

            this["labAreaScore" + i].text = "";
            this["labAreaMulti" + i].text = "";
            this["labAreaNoBet" + i].visible = false;
            this["labAreaScore" + i].visible = false;
            this["labAreaMulti" + i].visible = false;
        }
        // 初始化发牌索引
        this.currentCardIdx = 0;
        this.currentCardGroupIdx = 0;
        this.currentColorGroupIdx = 0;
        // 初始化 下注区域用户记录
        this.userBetAreaRecArray = [[], [], [], []];
    }

    public gameFree(): void
    {
        // this.report(P100GameSceneModule.request.gameEndOK);
        P100GameSceneData.getInstance().initData();
        this.initUI();
        // 设置状态显示
        var tipsInfo: any = {};
        tipsInfo.times = 3;
        tipsInfo.content = LanguageConfig.p100TakeRest;
        this.showGameStatusTips(tipsInfo);
    }

    // 显示加注面板
    // bAutoRiseIdx: 是否主动上升操作索引
    public updateJettonPanel(data: P100GameSceneData, bAutoRiseIdx:boolean): void
    {
        if (data.getSelfBetTotalValue() < data.getBankerScore())
        {
            this.setOperatePanelState(data, bAutoRiseIdx);
        }
        else
        {
            this.labJettonTips.text = LanguageConfig.p100JettonBankerMax;
            egret.Tween.removeTweens(this.groupJettonTips);
            this.groupJettonTips.visible = true;
            this.groupJettonTips.alpha = 1;
            var tw: egret.Tween = egret.Tween.get(this.groupJettonTips);
            tw.to({ alpha: 0 }, 2000);
        }
    }

    // 根据玩家金币显示操作面板状态
    private setOperatePanelState(data: P100GameSceneData, bAutoRiseIdx: boolean): void
    {
        var limit: number = data.getMaxJettonLimit() - data.getSelfBetTotalValue();
        this.tabBarOperate.visible = true;
        var selectedIdx: number = -1;
        for (var i: number = 0; i < this.tabIndexValue.length; ++i)
        {
            var item: eui.ItemRenderer = <eui.ItemRenderer>this.tabBarOperate.getElementAt(i);
            if (this.tabIndexValue[i] < limit)
            {
                selectedIdx = i;
                item.currentState = "";
                item.enabled = true;
            }
            else
            {
                item.currentState = "disabled";
                item.enabled = false;
            }
        }
        // 设置操作按钮状态
        if (selectedIdx >= 0)
        {
            // 根据参数bAutoRiseIdx判断是否只主动下降索引，不主动上升索引( 玩家自己点的称之为 被动)
            if (bAutoRiseIdx || this.tabBarOperate.selectedIndex > selectedIdx)
            {
                this.tabBarOperate.selectedIndex = selectedIdx;
            }
        }
        else
        {
            // 无法操作，提示已达下注最大值
            this.labJettonTips.text = LanguageConfig.p100JettonMax;
            egret.Tween.removeTweens(this.groupJettonTips);
            this.groupJettonTips.visible = true;
            this.groupJettonTips.alpha = 1;
            var tw: egret.Tween = egret.Tween.get(this.groupJettonTips);
            tw.to({ alpha: 0 }, 2000);
        }
    }

    // 用户下注
    // {"lJettonScore":200,"wChairID":6,"cbJettonArea":1,"bIsAndroid":1}
    public playerPlaceJetton(jettonInfo: any, userIdx:number): void
    {
        this.doJettonTween(jettonInfo, userIdx);
    }

    // 做玩家下注动作
    public doJettonTween(jettonInfo: any, userIdx:number): void
    {
        var jettonArea: number = jettonInfo.cbJettonArea - 1;
        if (this.userBetAreaRecArray[jettonArea].indexOf(userIdx) == -1)
        {
            this.userBetAreaRecArray[jettonArea].push(userIdx);
        }
        var groupBetArea: eui.Group = this.groupBetAreaArray[jettonArea];
        var startPoint: egret.Point = this.userSeatArray[userIdx].localToGlobal();
        startPoint = this.groupCenter.globalToLocal(startPoint.x, startPoint.y);
        var coinNums: number = this.getCoinCountByChips(jettonInfo.lJettonScore);
        for (var i: number = 0; i < coinNums; i++)
        {
            var endPoint: egret.Point = this.getRandomPoint(groupBetArea);
            endPoint = groupBetArea.localToGlobal(endPoint.x, endPoint.y);
            endPoint = this.groupCenter.globalToLocal(endPoint.x, endPoint.y);
            var coin: eui.Image = new eui.Image("jinbiimg_png");
            coin.x = startPoint.x;
            coin.y = startPoint.y;
            coin.touchEnabled = false;
            var tw: egret.Tween = egret.Tween.get(coin);
            tw.to({ x: endPoint.x, y: endPoint.y }, 900, egret.Ease.quartOut);
            this.groupCenter.addChild(coin);
            this.betCoinAreaArray[jettonInfo.cbJettonArea - 1].push(coin);
        }
    }

    // 根据一个控件获得一个位于该控件内的随机的点
    private getRandomPoint(img: eui.Group): egret.Point
    {
        var imgW: number = img.width;
        var imgH: number = img.height;
        var rndW: number = base.Utils.getRandom(0, imgW * 0.6);
        var rndH: number = base.Utils.getRandom(0, imgH * 0.6);

        var point: egret.Point = new egret.Point(img.x + rndW, img.y + rndH);
        return point;
    }

    private getCoinCountByChips(nums: number): number
    {
        var count: number = 0
        if (nums < 1000)
        {
            count = 1
        }
        else if (nums < 10000)
        {
            count = 2
        }
        else if (nums < 100000)
        {
            count = 3
        }
        else if (nums < 500000)
        {
            count = 4
        }
        else if (nums < 500000)
        {
            count = 5
        }
        else
        {
            count = 6
        }
        return count;
    }

    public upJettonInfo(jettonInfo: any, data: P100GameSceneData): void
    {
        // 区域总下注
        var totalBetValueArray: any[] = data.getTotalBetValueArray();
        for (var i: number = 0; i < totalBetValueArray.length; ++i)
        {
            var curBitLab: eui.BitmapLabel = this.totalBetBitLabArray[i];
            var curValue: number = totalBetValueArray[i];
            if (curValue > 0)
            {
                curBitLab.visible = true;
                curBitLab.text = base.Utils.formatCurrency(curValue);
            }
            else
            {
                curBitLab.visible = false;
                curBitLab.text = "";
            }
        }
        // 个人下注
        var selfBetValueArray: any[] = data.getSelfBetValueArray();
        for (var i: number = 0; i < selfBetValueArray.length; ++i)
        {
            var curSelfBitLab: eui.Label = this.selfBetLabelArray[i];
            var curSelfValue: number = selfBetValueArray[i];
            if (curSelfValue > 0)
            {
                curSelfBitLab.parent.visible = true;
                curSelfBitLab.text = curSelfValue.toString();
            }
            else
            {
                curSelfBitLab.parent.visible = false;
                curSelfBitLab.text = "";
            }
        }
    }

        // 隐藏下注面板
    public hideJettonPanel(): void
    {
        this.tabBarOperate.visible = false;
    }

        // 显示开牌
    public showOpenCard(): void
    {
        // // 展示发牌动作
        // this.doSendCard();
        // this.sendCardInterval = egret.setInterval(this.doSendCard, this, 80);
        this.gameEffectPanel.playSendCardEffect(this.sendCardPoint, this.showCardArray, this.beginShowCard.bind(this));
    }

    // // 发牌动作
    // private doSendCard(): void
    // {
    //     var componentCard: eui.Component = this.showCardArray[this.currentCardGroupIdx];
    //     var imgCard: eui.Image = componentCard["card" + this.currentCardIdx.toString()];
    //     var endPoint: egret.Point = new egret.Point(imgCard.x, imgCard.y);
    //     var sendGlobalPoint: egret.Point = this.sendCardPoint.localToGlobal();
    //     var startPoint: egret.Point = componentCard.globalToLocal(sendGlobalPoint.x, sendGlobalPoint.y);
    //     imgCard.visible = true;
    //     imgCard.x = startPoint.x;
    //     imgCard.y = startPoint.y;
    //     var tw: egret.Tween = egret.Tween.get(imgCard);
    //     tw.to({ x: endPoint.x, y: endPoint.y }, 120);
    //     this.currentCardGroupIdx++;
    //     if (this.currentCardGroupIdx >= this.showCardArray.length)
    //     {
    //         this.currentCardGroupIdx = 0;
    //         this.currentCardIdx++;
    //     }
    //     if (this.currentCardIdx >= this.CARD_NUMS)
    //     {
    //         egret.clearInterval(this.sendCardInterval);
    //         // 发牌完成通知模块调度开牌
    //         this.beginShowCard(P100GameSceneData.getInstance());
    //     }
    // }

    private beginShowCard(): void
    {
        var data: P100GameSceneData = P100GameSceneData.getInstance();
        this.showCardInterval = egret.setInterval(this.doShowCard, this, 500, data);
    }


    // 执行显示牌值动作
    private doShowCard(data: P100GameSceneData): void
    {
        var componentCard: eui.Component = this.showCardArray[this.currentColorGroupIdx];
        for (var i: number = 0; i < this.CARD_NUMS; ++i)
        {
            var imgCard: eui.Image = componentCard["card" + i.toString()];
            imgCard.visible = true;
            var tw: egret.Tween = egret.Tween.get(imgCard);
            tw.to({ scaleX: 0 }, 150);
        }
        this.timeoutSignArray["showCardValue"] = egret.setTimeout(this.showCardValue, this, 150, data);
    }

    private showCardValue(data: P100GameSceneData): void
    {
        var cardDataArray: any[] = data.getGameEndRes().cbTableCardArray;
        var cardTypeArray: any[] = data.getGameEndRes().cbCardType;
        var componentCard: eui.Component = this.showCardArray[this.currentColorGroupIdx];
        var cardData: number[] = cardDataArray[this.currentColorGroupIdx];
        for (var j: number = 0; j < cardData.length; ++j)
        {
            var imgCard: eui.Image = componentCard["card" + j.toString()];
            imgCard.source = "card_" + cardData[j] + "_png";
            imgCard.scaleX = 1;
            var imgCardType: eui.Image = this.showCardTypeArray[this.currentColorGroupIdx];
            imgCardType.source = "niu" + cardTypeArray[this.currentColorGroupIdx] + "_png";
            imgCardType.visible = true;
        }
        // 显示当前区域结算信息
        this.showBetAreaRes(data);
        // 切换到下一组
        this.currentColorGroupIdx++;
        if (this.currentColorGroupIdx >= this.showCardArray.length)
        {
            egret.clearInterval(this.showCardInterval);
            this.beginShowGameResult(P100GameSceneData.getInstance())
        }

    }

    // 准备显示结算框
    private beginShowGameResult(data: P100GameSceneData): void
    {
        this.timeoutSignArray["showGameResult"] = egret.setTimeout(this.showGameResult, this, 800, data);
    }

        // 显示结算框
    private showGameResult(data: P100GameSceneData): void
    {
        this.groupResult.visible = true;
        this.labSelfRes.text = data.getGameEndRes().lUserScore;
        this.labBankerRes.text = data.getGameEndRes().lBankerScore;
        this.timeoutSignArray["hideGameResult"] = egret.setTimeout(this.hideGameResult, this, 2500, data);
    }

        // 隐藏结算框
    private hideGameResult(data: P100GameSceneData): void
    {
        this.groupResult.visible = false;
        this.labSelfRes.text = "";
        this.labBankerRes.text = "";
        // this.report(P100GameSceneModule.request.showGameResultOK);
        this.beginShowResChips(P100GameSceneData.getInstance());
    }

    // 展示结算时的金币移动
    public beginShowResChips(data: P100GameSceneData): void
    {
        var allCardType: number[] = data.getGameEndRes().cbCardType;
        var nBankerType: number = allCardType[0];
        var nUserTypeArray: number[] = [];
        for (var i: number = 1; i < allCardType.length; ++i)
        {
            nUserTypeArray.push(allCardType[i]);
        }
        // 移动庄家输给玩家的金币
        var bBankerLose: boolean = false;
        for (var i: number = 0; i < nUserTypeArray.length; ++i)
        {
            if (nBankerType < nUserTypeArray[i])
            {
                this.moveChipsFromBankerToBetArea(i, this.betCoinAreaArray[i].length);
                bBankerLose = true;
            }
        }
        // 将金币从下注区域移动给玩家 设定延时是为了等待庄家移动到下注区的动作完成
        var nDelayTimes: number = 100;
        if (bBankerLose)
        {
            nDelayTimes = 1400;
        }
        this.timeoutSignArray["moveChipsFormBetAreaToWiner"] = egret.setTimeout(this.moveChipsFormBetAreaToWiner, this, nDelayTimes, data);
    }

    // 当一个区域的牌型比庄家大时，得从庄家手里移动该区域里同样数量的金币到该区域
    private moveChipsFromBankerToBetArea(areaIdx: number, coinNums: number)
    {
        for (var i: number = 0; i < coinNums; ++i)
        {
            var groupBetArea: eui.Group = this.groupBetAreaArray[areaIdx];
            var componentCard: eui.Component = this.showCardArray[0];
            var startPoint: egret.Point = componentCard.localToGlobal();
            startPoint = this.groupCenter.globalToLocal(startPoint.x, startPoint.y);

            var endPoint: egret.Point = this.getRandomPoint(groupBetArea);
            endPoint = groupBetArea.localToGlobal(endPoint.x, endPoint.y);
            endPoint = this.groupCenter.globalToLocal(endPoint.x, endPoint.y);
            var coin: eui.Image = new eui.Image("jinbiimg_png");
            coin.x = startPoint.x;
            coin.y = startPoint.y;
            var tw: egret.Tween = egret.Tween.get(coin);
            tw.wait(base.Utils.getRandom(0, 300)).set({ visible: true }).to({ x: endPoint.x, y: endPoint.y }, 1000, egret.Ease.quartOut);
            this.groupCenter.addChild(coin);
            this.betCoinAreaArray[areaIdx].push(coin);
        }
    }

    // 移动金币给牌型大的那一家
    private moveChipsFormBetAreaToWiner(data: P100GameSceneData): void
    {
        var allCardType: number[] = data.getGameEndRes().cbCardType;
        var nBankerType: number = allCardType[0];
        var nUserTypeArray: number[] = [];
        for (var i: number = 1; i < allCardType.length; ++i)
        {
            nUserTypeArray.push(allCardType[i]);
        }

        for (var i: number = 0; i < nUserTypeArray.length; ++i)
        {
            if (nBankerType < nUserTypeArray[i])
            {
                this.moveChipsFormBetAreaToPlayer(i, this.betCoinAreaArray[i].length);
            }
            else
            {
                this.moveChipsFormBetAreaToBanker(i, this.betCoinAreaArray[i].length)
            }
        }
    }

    // 移动下注区域的金币到庄家身上
    private moveChipsFormBetAreaToBanker(areaIdx: number, coinNums: number)
    {
        var componentCard: eui.Component = this.showCardArray[0];
        var endPoint: egret.Point = componentCard.localToGlobal();
        endPoint = this.groupCenter.globalToLocal(endPoint.x, endPoint.y);
        var betCoinArea: eui.Image[] = this.betCoinAreaArray[areaIdx];
        for (var i: number = 0; i < betCoinArea.length; ++i)
        {
            var coin: eui.Image = betCoinArea[i];
            var tw: egret.Tween = egret.Tween.get(coin);
            tw.wait(base.Utils.getRandom(0, 300)).set({ visible: true }).to({ x: endPoint.x, y: endPoint.y }, 1000, egret.Ease.quartOut);
        }
    }

    // 移动下注区域的金币到玩家身上
    private moveChipsFormBetAreaToPlayer(areaIdx: number, coinNums: number)
    {
        for (var x: number = 0; x < this.betCoinAreaArray[areaIdx].length; ++x)
        {
            var coin: eui.Image = this.betCoinAreaArray[areaIdx][x];
            this.groupCenter.removeChild(coin);
        }
        this.betCoinAreaArray[areaIdx].length = 0;
        var groupBetArea: eui.Group = this.groupBetAreaArray[areaIdx];
        var curAreaBetUser: number[] = this.userBetAreaRecArray[areaIdx];
        for (var x: number = 0; x < curAreaBetUser.length; ++x)
        {
            var betUserSeat: how.Button = this.userSeatArray[curAreaBetUser[x]];
            var endPoint: egret.Point = betUserSeat.localToGlobal();
            endPoint = this.groupCenter.globalToLocal(endPoint.x, endPoint.y);
            for (var j: number = 0; j < 5; ++j)
            {
                var startPoint: egret.Point = this.getRandomPoint(groupBetArea);
                startPoint = groupBetArea.localToGlobal(startPoint.x, startPoint.y);
                startPoint = this.groupCenter.globalToLocal(startPoint.x, startPoint.y);

                var coin: eui.Image = new eui.Image("jinbiimg_png");
                coin.x = startPoint.x;
                coin.y = startPoint.y;
                coin.visible = false;
                this.groupCenter.addChild(coin);
                var tw: egret.Tween = egret.Tween.get(coin);
                this.betCoinAreaArray[areaIdx].push(coin);
                tw.wait(base.Utils.getRandom(0, 300)).set({ visible: true }).to({ x: endPoint.x, y: endPoint.y }, 1000, egret.Ease.quartOut).set({ visible: false });
            }
        }
    }

    private showBetAreaRes(data: P100GameSceneData): void
    {
        if (this.currentColorGroupIdx >= 1)
        {
            var endRes: any = data.getGameEndRes();
            var multi: number = 1;
            var idx: number = this.currentColorGroupIdx - 1;
            if (endRes.gameRecord[idx] == 0)
            {
                multi = this.getMultiByCardType(endRes.cbCardType[0]);
                var score = data.getSelfBetValueArray()[idx] * multi;
                this["labAreaScore" + idx].text = "-" + base.Utils.formatCurrency(score);
                this["labAreaScore" + idx].textColor = 0xFF0000;
                this["labAreaMulti" + idx].textColor = 0xFF0000;
            }
            else
            {
                multi = this.getMultiByCardType(endRes.cbCardType[idx]);
                var score = data.getSelfBetValueArray()[idx] * multi;
                this["labAreaScore" + idx].text = "+" + base.Utils.formatCurrency(score);
                this["labAreaScore" + idx].textColor = 0xFFCF6E;
                this["labAreaMulti" + idx].textColor = 0xFFCF6E;
            }
            this["labAreaMulti" + idx].text = "×" + multi;
            if (data.getSelfBetValueArray()[idx] > 0)
            {
                this["labAreaScore" + idx].visible = true;
                this["labAreaMulti" + idx].visible = true;
            }
            else
            {
                this["labAreaNoBet" + idx].visible = true;
            }
        }
    }

    // 根据牌型获得倍数
    private getMultiByCardType(cardType: number): number
    {
        var nMulti: number = 1;
        if (cardType <= 6)
        {
            nMulti = 1;
        }
        else if (cardType <= 9)
        {
            nMulti = 2;
        }
        else
        {
            nMulti = 3;
        }
        return nMulti;
    }


    public betAreaClick(event: egret.TouchEvent): void
    {
        var areaIdx: number = this.groupColorAreaArray.indexOf(event.currentTarget);;
        var sendMsg: any = {};
        sendMsg.cbJettonArea = areaIdx + 1;
        sendMsg.lJettonScore = this.tabIndexValue[this.tabBarOperate.selectedIndex];

        WebSocketInstance.getHallSocket().send(CMDConfig.SEND_PLACE_JETTON, sendMsg);
    }

    public onTrendButtonBtnClick(event: egret.TouchEvent): void
    {
        // this.report(P100GameSceneModule.request.trendClick);
    }

    public onReqBankerClick(event: egret.TouchEvent): void
    {
        // this.report(P100GameSceneModule.request.reqBankerClick);
    }

    public onBackButton(event: egret.TouchEvent): void
    {
        // this.report(P100GameSceneModule.request.onBackButton);
    }

    public onUserBtnClick(event: egret.TouchEvent): void
    {
        // this.report(P100GameSceneModule.request.onlineUserClick);
    }

    public setBankerDataAndShow(data: any): void
    {
        P100GameSceneData.getInstance().setCurrentBanker(data);
        this.showBanker(P100GameSceneData.getInstance());
    }

    public showBanker(data: P100GameSceneData): void
    {
        var bankerData: base.UserData = data.getCurrentBankerData();
        if (bankerData)
        {
            this.labBankerName.text = bankerData.nickName;
            this.labBankerScore.text = "labBankerScore"; //base.Utils.formatCurrency(bankerData.UserScoreInfo.lScore);
            this.headLoader.source = "";//DataConfig.headID[bankerData.faceID].image;
        }
    }

    protected setGameStateTips(nTime: number, strTips: string): void
    {
        var tipsInfo: any = {};
        tipsInfo.times = nTime;
        tipsInfo.content = strTips;
        this.showGameStatusTips(tipsInfo);
    }

    public showGameStatusTips(data: any): void
    {
        this.groupGameStatus.visible = true;
        this.currentStatusTips = data.content;
        this.countDownTime = data.times;
        if (this.intervalSign != -1)
        {
            egret.clearInterval(this.intervalSign);
        }
        this.countDown();
        this.intervalSign = egret.setInterval(this.countDown, this, 1000);
        this.labStatus.visible = true;
    }

    private countDown(): void
    {
        this.labStatus.text = this.currentStatusTips + this.countDownTime + LanguageConfig.UNIT_SECOND;
        this.countDownTime--;
        if (this.countDownTime < 0)
        {
            egret.clearInterval(this.intervalSign);
            this.intervalSign = -1;
            this.groupGameStatus.visible = false;
            this.labStatus.text = "";
        }
    }
}